package com.lzh.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author zhehen.lu
 * @date 2025/8/3 13:51
 */
public class EasyExcelBigDecimalDiv10000Floor2BitBigDecimalConverter implements Converter<BigDecimal> {
    public EasyExcelBigDecimalDiv10000Floor2BitBigDecimalConverter() {
    }

    @Override
    public Class<?> supportJavaTypeKey() {
        return BigDecimal.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<BigDecimal> context) throws Exception {
        BigDecimal value = (BigDecimal)context.getValue();
        if (value == null) {
            return new WriteCellData(new BigDecimal("0"));
        } else {
            BigDecimal decimal = value.divide(new BigDecimal("10000"), 2, RoundingMode.FLOOR);
            return new WriteCellData(decimal);
        }
    }
}
